#ifndef __SDX_TXT_WRITTER__
#define __SDX_TXT_WRITTER__

namespace xv
{
	namespace textWritter
	{
		static bool write( xvalue_t & v );

		static bool write_null( xvalue_t & v )
		{
			printf("null");
			return true;
		}

		static bool write_int( xvalue_t & v )
		{
			int64_t i64 = 0;
			switch( v._type.bsiz )
			{
			case _8:	printf( "0x%X", (int8_t )v.integer ); break;
			case _16:	printf( "0x%X", (int16_t )v.integer ); break;
			case _32:	printf( "0x%X", (int32_t )v.integer ); break;
			case _64:	printf( "0x%I64X", (int64_t )v.integer ); break;
			};
			return true;
		};
		static bool write_real( xvalue_t & v )
		{
			printf( "%g", v.real );
			return true;
		};

		static bool write_lstr( xvalue_t& v )
		{
			refp<vStringA> str = v.asStringACP(false);
			if( !str ) return false;
			const char * ap = str->c_str();
			if( !ap ) ap = "";
			printf( "\"%s\"", ap );
			return true;
		};

		static bool write_utf8( xvalue_t& v )
		{
			refp<vStringA> str = v.asStringUTF8(false);
			if( !str ) return false;
			const char * ap = str->c_str();
			if( !ap ) ap = "";
			printf( "\"%s\"", ap );
			return true;
		};
		static bool write_unicode( xvalue_t& v )
		{
			refp<vStringW> str = v.asStringW(false);
			if( !str ) return false;
			const wchar_t * ap = str->c_str();
			if( !ap ) ap = L"";
			printf( "\"%ws\"", ap );
			return true;
		};

		static bool write_buffer( xvalue_t& v )
		{
			refp<vBuffer> buf = v.asBuffer(false);
			if( !buf ) return false;
			size_t cb = buf->length();
			uint8_t * u8p = buf->base();
			printf( "\'" );
			for( size_t i = 0; i < cb; ++ i )
				printf( "%02X", u8p[i] );
			printf( "\'" );
			return true;
		};

		static bool write_array( xvalue_t& v )
		{
			refp<vArray> arr = v.asArray(false);
			if( !arr ) return false;
			size_t ce = arr->length();
			struct dumper
			{
				static bool dump_pair( size_t idx, xv::AnyValue & item, bool last, void * p )
				{
					// write value
					xvalue_t tmp(item);
					write( tmp );
					if( !last )
						printf( ", " );
					return true;
				}
			};
			printf( "[" );
			arr->enum_it( dumper::dump_pair, 0 );
			printf( "]" );
			return true;
		};

		static bool write_dict( xvalue_t& v )
		{
			refp<vObject> obj = v.asObject(false);
			if( !obj ) return false;
			size_t cf = obj->length();
			struct dumper
			{
				static bool dump_pair( const char * name, xv::AnyValue & item, bool last, void * p )
				{
					printf( "%s : ", name );
					// write value
					xvalue_t tmp(item);
					write( tmp );
					if( !last ) printf( ", " );
					return true;
				}
			};
			printf( "{" );
			obj->enum_it( dumper::dump_pair, 0 );
			printf( "}" );
			return true;
		};
		static bool write( xvalue_t & v )
		{
			switch( v._type.type )
			{
			case dNull:		return write_null( v );
			case dInt:		return write_int( v );
			case dReal:		return write_real( v );
			case dLocalStr:	return write_lstr( v );
			case dUtf8Str:	return write_utf8( v );
			case dUniStr:	return write_unicode( v );
			case dArray:	return write_array( v );
			case dObject:	return write_dict( v );
			case dBuffer:	return write_buffer( v );
			}
			return false;
		};
	};
};

#endif
